﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<BlogPostEditViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
    <title>
        <% if (Model.Id == Guid.Empty)
           { %>
        <%= Model.Localize("NewPostPageTitle")%>
        <%}
           else
           { %>
        <%= Model.Localize("EditPostPageTitle")%>
        <% } %>
    </title>
    <%= Html.GetCssFile(Url.GetModuleCssPath("MvcEngine.Blog.BackOffice.css")) %>
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.SuccessNotification("successNotification", Model.Localize("PostSavedSuccessNotification"))%>
    <%= Html.ErrorNotification("errorNotification", Model.Localize("PostSavedErrorNotification"))%>
    <div class="page-title" id="post-header">
        <h1>
            <% if (Model.Id == Guid.Empty)
               { %>
            <%= Model.Localize("NewPostPageHeader")%>
            <%}
               else
               { %>
            <%= Model.Localize("EditPostPageHeader")%>
            <% } %>
        </h1>
    </div>
    <div class="page-menu">
        <%= Html.UrlActionLink(Model.Localize("BackLink"), Url.Blog(Model.BlogName))%>
        |
        <%= Html.UrlActionLink(Model.Localize("SaveLink"), "javascript:void(0);", "Save();return false;")%>
        <% if (Model.Id != Guid.Empty)
           { %>
            | <a href="<%= Url.PostDetails(Model.Id) %>" target="_blank"><%= Model.Localize("ViewLink")%></a>
        <% }%>
    </div>
    <div id="post-edit-form">
        <div id="post-edit-form-content">
            <% using (Html.BeginForm(Model.Id != Guid.Empty ? Url.EditPost(Model.Id) : Url.NewPost(), new { id = "editPostForm", enctype = "multipart/form-data" }))
               {%>
            <div class="form-element">
                <%= Html.TextBox("Title", Model.Title)%>
            </div>
            <div class="form-element">
                <% Html.RenderEditor("CKEditor", "PostContent", Model.PostContent); %>
            </div>
            <div class="form-element">
                <label for="UploadFile">
                    <%= Model.Localize("UploadFileLabel") %></label>
                <input type="file" id="UploadFile" name="file" />
                <%= Html.SubmitButton(Model.Localize("UploadButtonLink"), "editPostForm", Model.Id == Guid.Empty ? Url.PostUploadFile() : Url.PostUploadFile(Model.Id), new { @class = "SubmitButtonClass" })%>
            </div>
            <div class="form-element">
                <label for="Slug">
                    <%= Model.Localize("SlugLabel") %></label>
                <%= Html.TextBox("Slug", Model.Slug)%>
            </div>
            <div class="form-element">
                <label for="Author">
                    <%= Model.Localize("AuthorLabel")%></label>
                <%= Html.TextBox("Author", Model.Author)%>
            </div>
            <div class="widget summary">
                <h3>
                    <%= Model.Localize("DescriptionWidget")%></h3>
                <div class="sideBarList">
                    <%= Html.TextArea("Description", Model.Description)%>
                    <small>
                        <%= Model.Localize("DescriptionWidgetDescription")%></small>
                </div>
            </div>
            <div class="widget keywords">
                <h3>
                    <%= Model.Localize("KeywordsWidget")%></h3>
                <%= Html.TextArea("Keywords", Model.Keywords)%>
                <small>
                    <%= Model.Localize("KeywordsWidgetDescription")%></small>
            </div>
        </div>
        <div id="post-edit-form-sidebar">
            <div class="widget publish">
                <h3>
                    <%= Model.Localize("PublishWidget")%></h3>
                <div class="state">
                    <%= Model.Localize("PublishOnLabel")%>
                    <div id="published-date">
                        <%= Html.TextBox("PublishedDate", Model.PublishedDate.HasValue ? Model.PublishedDate.Value.ToShortDateString(): null, new { @class="pub-date" })%>
                        <%= Html.TextBox("PublishedTime", Model.PublishedDate.HasValue ? Model.PublishedDate.Value.ToShortTimeString() : null, new { @class = "pub-time" })%>
                    </div>
                </div>
            </div>
            <div class="widget categories">
                <h3>
                    <%= Model.Localize("CategoriesWidget")%></h3>

                <ul>
                    <%
                        int i = 0;
                        foreach (var item in Model.Categories)
                        {
                    %>
                    <li>
                        <%= Html.Hidden("Categories[" + i.ToString() + "].Key", item.Id)%>
                        <%= Html.CheckBox("Categories[" + i.ToString() + "].Value", Model.PostCategories == null ? false : Model.PostCategories.Any(p => p.Id == item.Id) , new { id = item.Name })%>
                        <%= Html.Label(item.Name) %>
                    </li>
                    <%
                        i++;
                      } %>
                </ul>
<%--                <div class="add">
                    <input type="text" value='<%= Model.Localize("CategoryNameValue")%>' title='<%= Model.Localize("CategoryNameValue")%>'
                        id="categoryName" name="categoryName" basecolor="rgb(0, 0, 0)" style="color: rgb(153, 153, 153);" />
                    <button name="addCategory">
                        <%= Model.Localize("AddButton")%></button>
                </div>--%>
            </div>
            <div class="widget tags">
                <h3>
                    <%= Model.Localize("TagsWidget")%></h3>
                <% if (Model.Tags.Count > 0)
                   {     
                %>
                <ul>
                    <%
                        i = 0;
                        foreach (var item in Model.Tags)
                        {
                    %>
                    <li>
                        <%= Html.Hidden("Tags[" + i.ToString() + "].Key", item.Id)%>
                        <%= Html.CheckBox("Tags[" + i.ToString() + "].Value", Model.PostTags == null ? false : Model.PostTags.Any(p => p.Id == item.Id), new { id = item.Name + i.ToString() })%>
                        <%= Html.Label(item.Name) %>
                    </li>
                    <%
                        i++;
              } %>
                </ul>
                <%
                    } 
                %>
<%--                <div class="add">
                    <input type="text" value='<%= Model.Localize("TagNameValue")%>' title='<%= Model.Localize("TagNameValue")%>'
                        name="tagName" basecolor="rgb(0, 0, 0)" style="color: rgb(153, 153, 153);" />
                    <button name="addTag">
                        <%= Model.Localize("AddButton")%></button>
                </div>--%>
            </div>
            <div class="widget annotations">
                <h3>
                    <%= Model.Localize("AnnotationsWidget")%></h3>
                <div class="state">
                    <%= Model.Localize("CommentCountLabel")%>
                    <strong><%= Model.CommentCount %></strong>
                </div>
<%--                <div class="state alt">
                    <%= Model.Localize("TrackPingbacksLabel")%>
                    <strong>0</strong>
                </div>--%>
                <div class="state alt last">
                    <%= Model.Localize("AllowNewLabel")%>
                    <strong>
                        <%= Html.RadioButton("IsCommentEnabled", "true", Model.IsCommentEnabled, new { id = "annsyes" })%>
                        <label for="annsyes">
                            <%= Model.Localize("YesLabel")%></label>
                        <%= Html.RadioButton("IsCommentEnabled", "false", !Model.IsCommentEnabled, new { id = "annsno" })%>
                        <label for="annsno">
                            <%= Model.Localize("NoLabel")%></label>
                    </strong>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript">
       
         function Save() {
            var options = {
                success: function (data) {
                    var json = JSON.parse(data);
                    $('.notification').hide('slow');
                    if (json.model != "valid") {
                        var result = "<ul>";
                        for (var i in json.errors) {
                            result += "<li>" + json.errors[i] + "</li>";
                        }
                        result += "</ul>";
                        $('#errorNotification').show('fast');
                        $('#errorNotification .notification-msg-content').html(result);
                    }
                    else 
                    {
                        if (json.model == "redirect") {
                            window.location.replace(json.redirectUrl);
                        }else{
                            $('#successNotification').show('fast');
                        }
                    }
                },
                error: function(request, textStatus, errorThrown) {
                    $('.notification').hide('fast');
                    $('#errorNotification').show('fast');
                }
            };
            $('#editPostForm').ajaxSubmit(options);
        }
       
       
       
//            var catCount = <%= Model.Categories.Count %>;
//       
//           $(document).ready(function() {

//                $('.categories button[name=addCategory]').click(function () {
//                    var nameValue = $("#categoryName")[0].value;
//                    var result = 
//                    { 
//                        name: nameValue,
//                        blogId: "<%= Model.Id %>",
//                        <%= MvcEngine.Mvc.AntiForgeryToken.TokenName %>: "<%= Html.GetAntiForgeryToken(m => m.AntiForgeryToken) %>" 
//                    };
//                    
//                    $.post("<%= Url.NewCategory() %>", result, function(data){
//                        var cat = $.trim($('input[name=categoryName]').val());
//                        if (cat.length < 1) 
//                            return false;
//                        $('.categories ul').append("<li><input id='Categories_" + catCount + "__Key' type='hidden' value='" + data.Id + "' name='Categories[" + catCount + "].Key'/><input id='" + cat + "' type='checkbox' name='Categories[" + catCount + "].Value' value='true' checked='checked' /><input type='hidden' value='false' name='Categories[" + catCount + "].Value'/><label for='" + cat + "'>" + cat + "</label></li>");
//                        
//                        scrollIntoView($('.categories ul li:last'), $('.categories ul'));
//                        flashit($('.categories ul li:last'), 'greenbg', 900, 2);
//                                        
//                        $('.categories input[name=categoryName]').val('');
//                        catCount++;                    
//                    }, "json");
//                    
//                    return false;
//                });
//           });
    </script>

    <%= Html.MvcEngineAntiForgeryToken(m => m.AntiForgeryToken) %>
    <%} %>
</asp:Content>
